#include "simpleSocket.h"

int socket_init(char * addr, int port){

    int clifd;
    struct sockaddr_in servaddr;
    clifd = socket(AF_INET, SOCK_STREAM, 0);
    if(clifd == -1){
        fprintf(stderr, "Socket init error\n");
        return -1;
    }

    bzero(&servaddr, sizeof(servaddr));

    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port);

    int retval = inet_pton(AF_INET, addr, &servaddr.sin_addr);

    if(retval == 0){
        fprintf(stderr, "Invalid address\n");
        return -1;
    }

    int retcon = connect(clifd, (sockaddr * )&servaddr, sizeof(servaddr));
    if(retcon != 0){
        fprintf(stderr, "Connect Failed\n");
        return -1;
    }

    fprintf(stdout, "connect established, ready to transfer data..\n");

    return clifd;
}


void socket_close(int fd){
    close(fd);
}
